#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stropts.h>
#include<linux/fb.h>
#include <sys/mman.h>

#define LCD "/dev/fb0"
typedef unsigned int* lcd;

int size=0;
int lcdfd=-1;

lcd open_lcd(){
    lcdfd = open(LCD,O_RDWR);
    if(-1 == lcdfd){
        perror("open err");
        return NULL;
    }
    printf("设备打开成功\n");

    struct fb_var_screeninfo info;
    int ret = ioctl(lcdfd,FBIOGET_VSCREENINFO,&info);

    if(-1 == ret){
        perror("获取设备信息失败");
        return NULL;
    }

    size=info.xoffset*info.yoffset*info.bits_per_pixel/8;

    mmap(NULL,size,PROT_READ | PROT_WRITE,MAP_SHARED,lcdfd,0);



    return p;
}

int close_lcd(lcd p){

    int ret = munmap(p,size);
    if(-1 == ret){
        perror("解除映射失败");
    }
    ret = close(lcdfd);
    if(-1 == ret){
        perror("关闭设备失败\n");
        return -1;
    }
    printf("设备已关闭\n");
    return 0;

}

int main(int argc,char const argv[]){

    lcd p=open_lcd();
    close_lcd(p);

    return 0;

}